package cn.itcast.controller.module;

import cn.itcast.base.BaseController;
import cn.itcast.domain.Module;
import cn.itcast.service.module.ModuleService;
import cn.itcast.utils.UtilFuns;
import com.github.pagehelper.PageInfo;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import java.util.List;
import java.util.UUID;

@Controller
@RequestMapping("/system/module")
public class ModuleController extends BaseController {

    @Autowired
    private ModuleService moduleService;


    @RequestMapping(name = "pageHelper插件分页查询菜单",value = "/list")
    @RequiresPermissions("模块管理")
    public String findAll(@RequestParam(defaultValue = "1") int pageNum, @RequestParam(defaultValue = "5") int pageSize){
        PageInfo pb = moduleService.findAll(pageNum, pageSize);
        request.setAttribute("pb",pb);
        return "system/module/module-list";

    }

    @RequestMapping(name = "到新建菜单页面",value = "/toAdd")
    public String toAdd(){
        // 查询所有的模块名
        List<Module> list=moduleService.findModule();
        request.setAttribute("menus",list);
        // 带到页面去展示
        return "system/module/module-add";
    }


    @RequestMapping(name = "菜单保存/菜单更新",value = "/edit")
    public String edit(Module module){

    

        if(UtilFuns.isEmpty(module.getId())){
            // 新增
            module.setId(UUID.randomUUID().toString());
            moduleService.save(module);
        }else{
            // 更新
            moduleService.update(module);

        }

        return "redirect:/system/module/list.do";
    }



    @RequestMapping(name = "到修改菜单页面回显数据",value = "/toUpdate")
    public String toUpdate(String id){
        Module module=moduleService.findById(id);
        request.setAttribute("module",module);

        List<Module> list=moduleService.findModule();
        request.setAttribute("menus",list);
        return "system/module/module-update";
    }


    @RequestMapping(name = "菜单数据删除",value = "/delete")
    public String delete(String id){

        moduleService.delete(id);

        return "redirect:/system/module/list.do";
    }




}
